<!DOCTYPE html>
<html lang="en-us">
  <head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8">
  <meta http-equiv="X-UA-Compatible" content="chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="robots" content="noodp"/>
  <meta name="author" content="Mr.wei">
  
  
  
  <link rel="prev" href="http://blog.mr-wei.cn/2019/shell-%E5%BE%AA%E7%8E%AF/" />
  <link rel="next" href="http://blog.mr-wei.cn/2019/cadence-allegro-%E5%BF%AB%E6%8D%B7%E9%94%AE/" />
  <link rel="canonical" href="http://blog.mr-wei.cn/2019/shell-%E6%B5%8B%E8%AF%95%E5%91%BD%E4%BB%A4/" />
  <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
  <link rel="manifest" href="/site.webmanifest">
  <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
  <meta name="msapplication-TileColor" content="#da532c">
  <meta name="theme-color" content="#ffffff">
  <title>
       
       
           shell 测试命令 | Mr.wei
       
  </title>
  <meta name="title" content="shell 测试命令 | Mr.wei">
    
  
  <link rel="stylesheet" href="/font/iconfont.css">
  <link rel="stylesheet" href="/css/main.min.css">


  
  
 

<script type="application/ld+json">
 "@context" : "http://schema.org",
    "@type" : "BlogPosting",
    "mainEntityOfPage": {
         "@type": "WebPage",
         "@id": "http:\/\/blog.mr-wei.cn"
    },
    "articleSection" : "posts",
    "name" : "shell 测试命令",
    "headline" : "shell 测试命令",
    "description" : "test 命令 语法 test 表达式\t#方式1 [表达式]\t#方式2  文件属性测试    表达式 说明     -b file 如果file文件存在且为块设备，则值为真   -c file 如果file文件存在且为字符设备，则值为真   -r file 如果file文件存在且为只读，则值为真   -w file 如果file文件存在且可写入，则值为真   -x file 如果file文件存在且可执行，则值为真   -s file 如果file文件存在且长度大于0，则值为真   -d file 如果file是一个目录，则值为真   -f file 如果file文件是一个普通文件，则值为真   -e file 如果file文件存在，则值为真    示例一 #!\/bin\/bash echo -n \x26quot;Please enter the file name:\x26quot; read FILENAME if test -e $FILENAME ;then ls -l $FILENAME else echo \x26quot;$FILENAME not found\x26quot; fi  示例二 #!",
    "inLanguage" : "en-us",
    "author" : "Mr.wei",
    "creator" : "Mr.wei",
    "publisher": "Mr.wei",
    "accountablePerson" : "Mr.wei",
    "copyrightHolder" : "Mr.wei",
    "copyrightYear" : "2019",
    "datePublished": "2019-09-03 10:51:46 \x2b0800 CST",
    "dateModified" : "2019-09-03 10:51:46 \x2b0800 CST",
    "url" : "http:\/\/blog.mr-wei.cn\/2019\/shell-%E6%B5%8B%E8%AF%95%E5%91%BD%E4%BB%A4\/",
    "wordCount" : "371",
    "keywords" : [ "shell", "Mr.wei"]
}
</script>

</head>

  


  <body class="">
    <div class="wrapper">
        <nav class="navbar">
    <div class="container">
        <div class="navbar-header header-logo">
        	<a href="javascript:void(0);" class="theme-switch"><i class="iconfont icon-xihuan"></i></a>&nbsp;<a href="http://blog.mr-wei.cn">Mr.wei</a>
        </div>
        <div class="menu navbar-right">
                
                
                <a class="menu-item" href="/posts/" title="">Blog</a>
                
                <a class="menu-item" href="http://balance.mr-wei.cn" title="">BalanceCar</a>
                
                <a class="menu-item" href="/categories/" title="">Categories</a>
                
                <a class="menu-item" href="/tags/" title="">Tags</a>
                
                <a class="menu-item" href="/about/" title="About">About</a>
                
        </div>
    </div>
</nav>
<nav class="navbar-mobile" id="nav-mobile" style="display: none">
     <div class="container">
        <div class="navbar-header">
            <div>  <a href="javascript:void(0);" class="theme-switch"><i class="iconfont icon-xihuan"></i></a>&nbsp;<a href="http://blog.mr-wei.cn">Mr.wei</a></div>
            <div class="menu-toggle">
                <span></span><span></span><span></span>
            </div>
        </div>
     
          <div class="menu" id="mobile-menu">
                
                
                <a class="menu-item" href="/posts/" title="">Blog</a>
                
                <a class="menu-item" href="http://balance.mr-wei.cn" title="">BalanceCar</a>
                
                <a class="menu-item" href="/categories/" title="">Categories</a>
                
                <a class="menu-item" href="/tags/" title="">Tags</a>
                
                <a class="menu-item" href="/about/" title="About">About</a>
                
        </div>
    </div>
</nav>







    	 <main class="main">
          <div class="container">
      		
<article class="post-warp" itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
        <h1 class="post-title" itemprop="name headline">shell 测试命令</h1>
        <div class="post-meta">
                Written by <a itemprop="name" href="http://blog.mr-wei.cn" rel="author">Mr.wei</a> with ♥ 
                <span class="post-time">
                on <time datetime=2019-09-03 itemprop="datePublished">September 3, 2019</time>
                </span>
                in
                <i class="iconfont icon-folder"></i>
                <span class="post-category">
                        
                </span>
        </div>
    </header>
    <div class="post-content">
        

        

        
        
     
          
          
          

          
          
          

          

<h2 id="test-命令">test 命令</h2>

<h3 id="语法">语法</h3>

<pre><code class="language-shell">test 表达式			#方式1
[表达式]			#方式2
</code></pre>

<!-- more -->

<h3 id="文件属性测试">文件属性测试</h3>

<table>
<thead>
<tr>
<th>表达式</th>
<th>说明</th>
</tr>
</thead>

<tbody>
<tr>
<td>-b file</td>
<td>如果file文件存在且为块设备，则值为真</td>
</tr>

<tr>
<td>-c file</td>
<td>如果file文件存在且为字符设备，则值为真</td>
</tr>

<tr>
<td>-r file</td>
<td>如果file文件存在且为只读，则值为真</td>
</tr>

<tr>
<td>-w file</td>
<td>如果file文件存在且可写入，则值为真</td>
</tr>

<tr>
<td>-x file</td>
<td>如果file文件存在且可执行，则值为真</td>
</tr>

<tr>
<td>-s file</td>
<td>如果file文件存在且长度大于0，则值为真</td>
</tr>

<tr>
<td>-d file</td>
<td>如果file是一个目录，则值为真</td>
</tr>

<tr>
<td>-f file</td>
<td>如果file文件是一个普通文件，则值为真</td>
</tr>

<tr>
<td>-e file</td>
<td>如果file文件存在，则值为真</td>
</tr>
</tbody>
</table>

<h4 id="示例一">示例一</h4>

<pre><code class="language-shell">#!/bin/bash
echo -n &quot;Please enter the file name:&quot;
read FILENAME
if test -e $FILENAME ;then
	ls -l $FILENAME
else
	echo &quot;$FILENAME not found&quot;
fi
</code></pre>

<h4 id="示例二">示例二</h4>

<pre><code class="language-shell">#!/bin/bash
echo -n &quot;Please enter the file name:&quot;
read FILENAME
if [ -e $FILENAME ] ;then
    ls -l $FILENAME
else
    echo &quot;$FILENAME not found&quot;
fi
</code></pre>

<h3 id="数值测试">数值测试</h3>

<table>
<thead>
<tr>
<th>表达式</th>
<th>说明</th>
</tr>
</thead>

<tbody>
<tr>
<td>n1 -eq n2</td>
<td>n1 等于 n2，则为真</td>
</tr>

<tr>
<td>n1 -ne n2</td>
<td>n1 不等于 n2，则值为真</td>
</tr>

<tr>
<td>n1 -gt n2</td>
<td>n1 大于 n2，则值为真</td>
</tr>

<tr>
<td>n1 -lt n2</td>
<td>n1 小于 n2，则值为真</td>
</tr>

<tr>
<td>n1 -ge n2</td>
<td>n1 大于等于 n2，则值为真</td>
</tr>

<tr>
<td>n1 -le n2</td>
<td>n1 小于等于 n2，则值为真</td>
</tr>
</tbody>
</table>

<h4 id="示例一-1">示例一</h4>

<pre><code class="language-shell">#!/bin/bash
echo -n &quot;Please enter the first number: &quot;
read N1
echo -n &quot;Please enter the second number: &quot;
read N2
if test $N1 -eq $N2 ;then
	echo &quot;Equal&quot;
else
	echo &quot;Not Equal&quot;
fi
</code></pre>

<h4 id="示例二-1">示例二</h4>

<pre><code class="language-shell">#!/bin/bash
echo -n &quot;Please enter the first number: &quot;
read N1
echo -n &quot;Please enter the second number: &quot;
read N2
if test [ $N1 -eq $N2 ] ;then
	echo &quot;Equal&quot;
else
	echo &quot;Not Equal&quot;
fi
</code></pre>

<h3 id="字符串测试">字符串测试</h3>

<table>
<thead>
<tr>
<th>表达式</th>
<th>说明</th>
</tr>
</thead>

<tbody>
<tr>
<td>-z s1</td>
<td>如果s1字符串的长度是0，则值为真</td>
</tr>

<tr>
<td>-n s1</td>
<td>如果s1字符串的长度不为0，则值为真</td>
</tr>

<tr>
<td>s1 = s2</td>
<td>如果s1字符串与s2字符串相等，则值为真</td>
</tr>

<tr>
<td>s1 != s2</td>
<td>如果s1字符串与s2字符串不相等，则值为真</td>
</tr>

<tr>
<td>s1</td>
<td>如果s1字符串不是空串，则值为真</td>
</tr>
</tbody>
</table>

<h4 id="示例一-2">示例一</h4>

<pre><code class="language-shell">#!/bin/bash
echo -n &quot;Please enter the first string: &quot;
read S1
echo -n &quot;Please enter the second string: &quot;
read S2
if test $S1 = $S2 ;then
	echo &quot;Equal&quot;
else
	echo &quot;Not Equal&quot;
fi
</code></pre>

<h4 id="示例二-2">示例二</h4>

<pre><code class="language-shell">#!/bin/bash
echo -n &quot;Please enter the first string: &quot;
read S1
echo -n &quot;Please enter the second string: &quot;
read S2
if test [ $S1 = $S2 ] ;then
	echo &quot;Equal&quot;
else
	echo &quot;Not Equal&quot;
fi
</code></pre>

<h3 id="逻辑运算">逻辑运算</h3>

<table>
<thead>
<tr>
<th>表达式</th>
<th>说明</th>
</tr>
</thead>

<tbody>
<tr>
<td>!</td>
<td>非操作</td>
</tr>

<tr>
<td>-a</td>
<td>与操作</td>
</tr>

<tr>
<td>-o</td>
<td>或操作</td>
</tr>
</tbody>
</table>

<h4 id="示例一-3">示例一</h4>

<pre><code class="language-shell">#!/bin/bash
echo -n &quot;Please enter a number: &quot;
read N1
if test $N1 -ge 10 -a $N1 -le 50 ;then
	echo &quot;10 &lt;= $N1 &lt;=50&quot;
else
	echo &quot;Not between 10 and 50&quot;
fi
</code></pre>

<h4 id="示例二-3">示例二</h4>

<pre><code class="language-shell">#!/bin/bash
echo -n &quot;Please enter a number: &quot;
read N1
if [ $N1 -ge 10 -a $N1 -le 50 ] ;then
	echo &quot;10 &lt;= $N1 &lt;=50&quot;
else
	echo &quot;Not between 10 and 50&quot;
fi
</code></pre>

<h1 id="参考资料">参考资料</h1>

<ul>
<li><font size=6>Linux命令行与shell脚本编程大全</font></li>
</ul>

    </div>

    <div class="post-copyright">
             
            <p class="copyright-item">
                <span>Author:</span>
                <span>Mr.wei </span>
                </p>
            
           
             
            <p class="copyright-item">
                    <span>Link:</span>
                    <a href=http://blog.mr-wei.cn/2019/shell-%E6%B5%8B%E8%AF%95%E5%91%BD%E4%BB%A4/>http://blog.mr-wei.cn/2019/shell-%E6%B5%8B%E8%AF%95%E5%91%BD%E4%BB%A4/</span>
            </p>
            
             
            <p class="copyright-item lincese">
                本文采用<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" #target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>进行许可
            </p>
            
    </div>

  
    <div class="post-tags">
        
            <section>
            <i class="iconfont icon-tag"></i>Tag(s): 
            
            <span class="tag"><a href="http://blog.mr-wei.cn/tags/shell/">
                    #shell</a></span>
            
            </section>
        
        <section>
                <a href="javascript:window.history.back();">back</a></span> · 
                <span><a href="http://blog.mr-wei.cn">home</a></span>
        </section>
    </div>

    <div class="post-nav">
        
        <a href="http://blog.mr-wei.cn/2019/shell-%E5%BE%AA%E7%8E%AF/" class="prev" rel="prev" title="Shell 循环"><i class="iconfont icon-left"></i>&nbsp;Shell 循环</a>
         
        
        <a href="http://blog.mr-wei.cn/2019/cadence-allegro-%E5%BF%AB%E6%8D%B7%E9%94%AE/" class="next" rel="next" title="Cadence Allegro 快捷键">Cadence Allegro 快捷键&nbsp;<i class="iconfont icon-right"></i></a>
        
    </div>

    <div class="post-comment">
          
                 
          
    </div>
</article>
          </div>
		   </main>
      <footer class="footer">
    <div class="copyright">
        &copy;
        
        <span itemprop="copyrightYear">2018 - 2019</span>
        
        <span class="with-love">
    	 <i class="iconfont icon-love"></i> 
         </span>
         
            <span class="author" itemprop="copyrightHolder"><a href="http://blog.mr-wei.cn">Mr.wei</a> | </span> 
         

         
            <a href="http://www.miibeian.gov.cn/" target="_blank" rel="external nofollow">冀ICP备 - 18032379号 </a> |
         
		  <span>Powered by <a href="https://gohugo.io/" target="_blank" rel="external nofollow">Hugo</a> & <a href="https://github.com/liuzc/leaveit" target="_blank" rel="external nofollow">LeaveIt</a></span> 
    </div>

	
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [['$','$'], ['\\(','\\)']],
            displayMath: [['$$','$$'], ['\[','\]']],
            processEscapes: true,
            processEnvironments: true,
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            TeX: {
                equationNumbers: { autoNumber: "AMS" },
                extensions: ["AMSmath.js", "AMSsymbols.js"]
            }
        }
    });
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>
	
	

</footer>












    
    
    <script src="/js/vendor_no_gallery.min.js" async=""></script>
    
  



     </div>
  </body>
</html>
